package com.atguigu.system.service.impl;


import com.atguigu.common.utils.RouterHelper;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.system.SysRoleMenu;
import com.atguigu.model.system.SysUser;
import com.atguigu.model.vo.AssginMenuVo;
import com.atguigu.model.vo.RouterVo;
import com.atguigu.system.exception.GuiguException;
import com.atguigu.system.mapper.SysMenuMapper;
import com.atguigu.system.mapper.SysRoleMenuMapper;
import com.atguigu.system.service.SysMenuService;
import com.atguigu.system.util.MenuHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author chen
 * @since 2024-03-09
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    // 菜单列表(树形)
    @Override
    public List<SysMenu> findNodes() {
        List<SysMenu> list = baseMapper.selectList(null);
        //菜单数据转换格式
        List<SysMenu> resultList = MenuHelper.buildTree(list);
        return resultList;
    }

    // 删除菜单
    @Override
    public void removeMenuById(String id) {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getParentId,id);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new GuiguException(201,"请先删除子菜单");
        }
        baseMapper.deleteById(id);
    }

    @Override
    public void updateStatus(String id, Integer status) {
        SysMenu sysMenu = baseMapper.selectById(id);
        sysMenu.setStatus(status);
        baseMapper.updateById(sysMenu);
    }

    // 根据角色分配菜单
    @Override
    public List<SysMenu> findSysMenuByRoleId(String roleId) {
        // 根据角色分配菜单
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getStatus,1);
        List<SysMenu> menuList = baseMapper.selectList(wrapper);
        // 根据角色id查询角色分配过的菜单列表
        LambdaQueryWrapper<SysRoleMenu> wrapperRoleMenu = new LambdaQueryWrapper<>();
        wrapperRoleMenu.eq(SysRoleMenu::getRoleId,roleId);
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(wrapperRoleMenu);
        //获取角色分配所有菜单ID
        ArrayList<String> menuIds = new ArrayList<>();
        for (SysRoleMenu roleMenu:sysRoleMenus) {
            menuIds.add(roleMenu.getMenuId());
        }
        // 数据处理：isSelect 如果菜单选中true,否则false
        // 拿着分配菜单id和所有菜单比对，有相同则让isSelect值为true
        for (SysMenu menu:menuList) {
            if (menuIds.contains(menu.getId())){
                menu.setSelect(true);
            }else{
                menu.setSelect(false);
            }
        }
        //菜单数据转换格式
        List<SysMenu> resultList = MenuHelper.buildTree(menuList);
        return resultList;
    }

    // 给角色分配权限
    @Override
    public void doAssign(AssginMenuVo assginMenuVo) {
        // 根据角色id删除菜单权限
        LambdaQueryWrapper<SysRoleMenu> wrapperRoleMenu = new LambdaQueryWrapper<>();
        wrapperRoleMenu.eq(SysRoleMenu::getRoleId,assginMenuVo.getRoleId());
        sysRoleMenuMapper.delete(wrapperRoleMenu);
        // 遍历菜单id列表，一个一个添加
        for (String menuId:assginMenuVo.getMenuIdList()) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(assginMenuVo.getRoleId());
            roleMenu.setMenuId(menuId);
            sysRoleMenuMapper.insert(roleMenu);
        }
    }

    // 根据userId查询菜单权限值
    @Override
    public List<RouterVo> getUserMenuList(String userId) {
        //admin是超级管理员，操作所有内容
        List<SysMenu> sysMenuList = null;
        //判断userId的值是1代表超级管理员，查询所有权限数据
        if("1".equals(userId)){
            QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
            wrapper.eq("status",1);
            wrapper.orderByAsc("sort_value");
            sysMenuList = baseMapper.selectList(wrapper);
        }else {
            //如果userId不是1，其它类型用户，查询这个用户权限
            sysMenuList = baseMapper.findMenuListUserId(userId);
        }
        // 构建树形结构
        List<SysMenu> sysMenuTressList = MenuHelper.buildTree(sysMenuList);
        //转换成前端路由要求格式数据
        List<RouterVo> routerVoList = RouterHelper.buildRouters(sysMenuTressList);
        return routerVoList;
    }

    // 根据userId查询按钮权限值
    @Override
    public List<String> getUserButtonList(String id) {
        List<SysMenu> sysMenuList = null;
        //超级管理员admin账号id为：1
        if("1".equals(id)){
            sysMenuList = baseMapper.selectList(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getStatus, 1));
        }else {
            sysMenuList = baseMapper.findMenuListUserId(id);
        }
        ArrayList<String> permissionList = new ArrayList<>();
        for (SysMenu sysMenu:sysMenuList) {
            if (sysMenu.getType() == 2)
            permissionList.add(sysMenu.getPerms());
        }
        return permissionList;
    }
}
